package com.candy.test;


import com.candy.dao.UserInfoMapper;
import com.candy.pojo.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class DBDriver {
    public static void main(String[] args) {
        test02();
    }

    public static void test01() {

        try {
//           读取配置文件
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            实例化SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           获取SqlSession
            SqlSession session= sqlSessionFactory.openSession();
//            参数一：指定要执行的sql（mapper文件的namesapce+方法的id构成）
//           UserInfo userinfo= session.selectOne("com.candy.UserInfoMapper.getUserInfoById",1);
//            System.out.println(userinfo);
            UserInfoMapper mapper=session.getMapper(UserInfoMapper.class);
            UserInfo userinfo=new UserInfo();
//            userinfo.setUsername("黑桃A");
//            userinfo.setUserpwd("12345678");
            userinfo.setUsertel("18988878881");
            userinfo.setUseraddress("山东济南");
            userinfo.setId(7);
//            int num=mapper.insertSelective(userinfo);
            int num=mapper.updateByPrimaryKeySelective(userinfo);

            System.out.println(num);
//            在执行增删改操作时，需要手动提交事务
            session.commit();



        } catch (IOException e) {
            e.printStackTrace();
        }


    }



    public static void test02() {

        try {
//           读取配置文件
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            实例化SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           获取SqlSession
            SqlSession session= sqlSessionFactory.openSession();
//            参数一：指定要执行的sql（mapper文件的namesapce+方法的id构成）
//           UserInfo userinfo= session.selectOne("com.candy.UserInfoMapper.getUserInfoById",1);
//            System.out.println(userinfo);
            UserInfoMapper mapper=session.getMapper(UserInfoMapper.class);
            UserInfo userinfo= mapper.selectByPrimaryKey(1);
            System.out.println(userinfo);

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
